<?php

class Email extends DBItem {
	public $Rcpt;
	public $From;
	public $Subject;
	public $Body;
	public $DateSent;
	public $idSentBy;

	public static function FillMetaClass(MetaClass $c){
		$c->SetDBTableName('oxy_emails');
		$c->From = MetaField::String()->WithAlias('XFrom');
		$c->Rcpt = MetaField::String();
		$c->Subject = MetaField::String();
		$c->Body = MetaField::String();
		$c->idSentBy = MetaField::ID();
		$c->DateSent = MetaField::DateTime()->WithLabel('DateSent');
	}

	protected function OnInit(){
		//$this->From = Lemma::SiteName() . ' <'. Oxygen::$smtp_email . '>';
	}	

	public final function Send(){
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
		$headers .= 'To: '. $this->Rcpt ."\r\n";
		$headers .= 'From: '. $this->From ."\r\n";

		$msg = '<html><head>';
		$msg .= '<meta http-equiv="Content-Type" content="text/html" charset="UTF-8" />';
		$msg .= '<title>'.$this->Subject.'</title>';
		$msg .= '</head><body>';
		$msg .= $this->Body;
		$msg .= '</body></html>';
		
		mail($this->Rcpt, $this->Subject, $msg, $headers);
//		if (!mail($this->Rcpt, $this->Subject, $msg, $headers))
//			throw new ApplicationException(Lemma::ErrorCannotSendEmail());
			
		$this->idSentBy = User::GetCurrent()->id;
		$this->DateSent = new DateTime();
		$this->Save();
	}
	
	

}
?>
